<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a><cite>活动管理</cite></a>
    <a lay-href="lottery/index">抽奖活动</a>
    <a><cite>查看抽奖名单</cite></a>
  </div>
</div>

<div class="layui-fluid">
  <div class="layui-card">
    <div class="layadmin-search">
      <form class="layui-form" method="post" lay-filter="form_lottery_user_search" id="filterForm">
        <script type="text/html" template lay-done="layui.data.done(d.data)">
          <div class="layui-inline">
            <div class="layui-form-label">关键字搜索</div>
            <div class="layui-input-inline">
              <input type="text" name="keyword" value="{{decodeURIComponent(layui.router().search.keyword || '')}}"
                     class="layui-input"
                     placeholder="请输入手机号">
            </div>
          </div>
          <div class="layui-inline layui-btn-group">
            <button type="submit" lay-submit class="layui-btn" lay-filter="submit_lottery_user_search">
              <i class="layui-icon layui-icon-search"></i>查询
            </button>
            <button type="submit" lay-submit class="layui-btn layui-btn-warm"
                    lay-filter="reset_lottery_user_search">
              <i class="layui-icon layui-icon-close"></i>重置
            </button>
          </div>
          <a id="btn_add_user" class="layui-btn layui-btn-normal">
            <i class="layui-icon layui-icon-add-1"></i>添加用户
          </a>
          <a class="layui-btn layui-table-link" id="btn_upload_users">
            <i class="layui-icon layui-icon-upload-circle"></i>批量导入
          </a>
          <div class="layui-inline layui-word-aux" style="padding-top: 20px">
            <a href="/assets/excel/抽奖名单导入模板.xls" class="layui-font-blue" style="text-decoration:underline;">下载导入模版</a>
          </div>
        </script>
      </form>
    </div>
    <div class="layui-card-body">
      <div id="table_lottery_user"></div>
      <script type="text/html" id="tpl_user">
        {{d.user.mobile || ''}}
        {{# if (d.auth) { }}
        ({{d.auth.real_name}})
        {{# } else if (d.user.nickname) { }}
        ({{d.user.nickname || ''}})
        {{# } }}
      </script>
      <script type="text/html" id="tpl_tool">
        <a class="layui-table-link" lay-event="edit">
          <i class="layui-icon layui-icon-edit"></i>
          编辑
        </a>
        <div class="ant-divider"></div>
        <a class="layui-table-link layui-font-red" lay-event="del">
          <i class="layui-icon layui-icon-delete"></i>
          删除
        </a>
      </script>
    </div>
  </div>
</div>

<script>
  layui.data.done = function () {
    layui.use(['fox_table'], function () {
      let $ = layui.$
        , fox_table = layui.fox_table
        , admin = layui.admin
        , form = layui.form
        , view = layui.view
        , upload = layui.upload
        , lottery_id = layui.router().search.id


      let resourceUrl = 'agent-admin/lottery_users?lottery_id=' + lottery_id

      // 列表查询
      fox_table.render({
        id: 'table_lottery_user'
        , url: resourceUrl
        , cols: [[
          {title: '用户', templet: '#tpl_user', align: "left", minWidth: 180}
          , {title: '用户等级', field: 'level', width: 100, align: "center"}
          , {title: '可抽奖次数', field: 'available_times', width: 100, align: "center"}
          , {title: '已抽奖次数', field: 'draw_times', width: 100, align: "center"}
          , {title: '操作', width: 180, align: 'center', fixed: 'right', toolbar: '#tpl_tool'}
        ]]
      });

      fox_table.toolbar(function (obj) {
        let data = obj.data;
        if (obj.event === 'edit') {
          showUserForm(data)
        }
        if (obj.event === 'del') {
          if (data.draw_count > 0) {
            layer.msg('已参与抽奖的用户信息无法删除');
            return;
          }
          layer.confirm('确认删除该用户数据?', function (index) {
            admin.del('agent-admin/lottery_users', data.id, function (res) {
              layer.close(index);
              fox_table.reload(); //重载表格
            })
          });
        }
      });

      upload.render({
        elem: '#btn_upload_users'
        , url: 'agent-admin/lotteries/' + lottery_id + '/import_users' //上传接口
        , exts: 'xls|xlsx'
        , headers: {
          Authorization: admin.getAccessToken('Bearer')
        }
        , done: function (res) {
          if (res.code !== 0) {
            layer.msg(res.msg);
            return false;
          }
          layer.msg('上传完成');
          fox_table.reload()
          return false;
        }
      });

      //搜索
      form.on('submit(submit_lottery_user_search)', function (d) {
        let data = d.field
        data.id = lottery_id
        fox_table.reload(data);
        return false;
      });
      //重置
      form.on('submit(reset_lottery_user_search)', function (d) {
        $("input[name='keyword']").val('');
        fox_table.reload({id: lottery_id});
        return false;
      });
      //手动添加用户
      $('#btn_add_user').click(function () {
        showUserForm();
      })

      //用户编辑
      function showUserForm(data) {
        data = data || {};
        data.lottery_id = lottery_id
        let minWidth = '500px';
        let minHeight = '350px';
        admin.popup({
          title: (data.id ? '编辑' : '添加') + '用户'
          , area: [minWidth, minHeight]
          , id: 'popup_lottery_user'
          , success: function (layero, index) {
            view(this.id).render('lottery/user_form', data).done(function () {
              form.on('submit(submit_lottery_user)', function (d) {
                let field = d.field
                admin.post('agent-admin/lottery_users', field, function (res) {
                  layer.close(index)
                  fox_table.reload();
                })
                return false;
              })
            })
          }
        })
      }
    })
  }
</script>

